package com.culture.config.wrapper;

import java.util.Date;
import java.util.Optional;
import java.util.Map;
import java.util.Objects;

import com.culture.config.common.util.BeanUtil;
import com.culture.config.common.util.DateTimeUtil;
import com.culture.config.common.util.HashUtil;
import com.culture.config.common.util.JSONUtil;
import com.culture.config.constant.AppInstanceStatus;
import com.culture.config.dao.entity.App;
import com.culture.config.dao.entity.AppInstance;
import com.culture.config.dao.entity.AppInstanceQuery;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;
import com.culture.config.util.AuthenticationUtil;

import org.apache.commons.collections.CollectionUtils;

import lombok.experimental.UtilityClass;

@UtilityClass
public class AppInstanceWrapper {
    public AppInstance wrapAppInstanceRegister(RegistrationPO param,App app ) {
		AppInstance appInstance = BeanUtil.copyProperties(param, AppInstance.class);
        Optional.ofNullable(app).ifPresent(e->appInstance.setAppId(e.getId()));
		appInstance.setServiceHash(HashUtil.md5Hex(appInstance.getManagementUrl()));
        appInstance.setHeartbeatTime(new Date());
        appInstance.setCurrentStatus(AppInstanceStatus.REGISTER.id());
		appInstance.setServiceInfo(JSONUtil.beanToJSONString(param.getMetadata()));
		AuthenticationUtil.setInsertFieldInfo(appInstance);        
		return appInstance;
	}

	public AppInstance wrapAppInstanceHeartbeat(AppInstance appInstance,RegistrationPO param) {
		AppInstance updateAppInstance = BeanUtil.copyProperties(param, AppInstance.class);
		updateAppInstance.setId(appInstance.getId());
		updateAppInstance.setServiceHash(HashUtil.md5Hex(param.getManagementUrl()));	
        updateAppInstance.setHeartbeatTime(new Date());
        updateAppInstance.setCurrentStatus(AppInstanceStatus.LIVE.id());
		AuthenticationUtil.setUpdateFieldInfo(updateAppInstance);
		return updateAppInstance;
	}
	
    public AppInstance wrapAppInstanceHeartbeat(String id) {		
		AppInstance updateAppInstance = new AppInstance(Long.parseLong(id));
        updateAppInstance.setHeartbeatTime(new Date());
        updateAppInstance.setCurrentStatus(AppInstanceStatus.LIVE.id());
		AuthenticationUtil.setUpdateFieldInfo(updateAppInstance);
		return updateAppInstance;
	}

	public AppInstance wrapAppInstanceUnregister(String id) {		
		AppInstance updateAppInstance = new AppInstance(Long.parseLong(id));
        updateAppInstance.setHeartbeatTime(new Date());
        updateAppInstance.setCurrentStatus(AppInstanceStatus.UNREGISTER.id());
		AuthenticationUtil.setUpdateFieldInfo(updateAppInstance);
		return updateAppInstance;
	}

	public AppInstanceQuery wrapAppInstanceQuery(AppInstancePagePO appInstancePagePO) {		
		AppInstanceQuery appInstanceQuery = new AppInstanceQuery(appInstancePagePO.getName(),appInstancePagePO.getAppId());
		if(CollectionUtils.isNotEmpty(appInstancePagePO.getStatus())){
			appInstanceQuery.setCurrentStatus(appInstancePagePO.getStatus().get(0));
		}	
		appInstanceQuery.setTimeOutMinutes(5);
		return appInstanceQuery;
	}

	public void wrapAppInstancePage(AppInstance appInstances, AppInstanceVO appInstanceVO) {		
		appInstanceVO.setServiceInfo(JSONUtil.jsonStringToMap(appInstances.getServiceInfo()));
		if(DateTimeUtil.isTimeExpired(appInstanceVO.getHeartbeatTime(), 6000L * 11L)){
			appInstanceVO.setCurrentStatus(AppInstanceStatus.PAUSE.id());
		}	
		if(DateTimeUtil.isTimeExpired(appInstanceVO.getHeartbeatTime(), 6000L * 30L)){
			appInstanceVO.setCurrentStatus(AppInstanceStatus.UNREGISTER.id());
		}			
	}

	public AppInstanceProxyVO wrapAppInstanceProxyVO(AppInstance appInstance, Map<String,Object> proxyResult){
		AppInstanceProxyVO appInstanceProxyPO = new AppInstanceProxyVO(appInstance.getId(),appInstance.getName());
		appInstanceProxyPO.setProxyResult(proxyResult);
		return appInstanceProxyPO;
	}

	public RegistrationVO wrapRegistrationVO(AppInstance appInstance){
		String id = Objects.nonNull(appInstance) ? String.valueOf(appInstance.getId()) : "0";	
		return new RegistrationVO(id);
	}
}
